package tool.codescan.visitors;

import java.util.List;

import tool.codescan.mappers.InnerClassMapper;
import tool.data.TypeDefinition;

import japa.parser.ast.PackageDeclaration;
import japa.parser.ast.body.BodyDeclaration;
import japa.parser.ast.body.ClassOrInterfaceDeclaration;
import japa.parser.ast.visitor.VoidVisitorAdapter;
import japa.parser.ast.CompilationUnit;

	/**
	 * @author jwb09119
	 * @param <A>
	 * @date 2013/12/10
	 * This class is a visitor class which passes over all .java files, recording information
	 * required before the main data gathering pass such as the names and locations of inner
	 * classes.
	 * 
	 * @date 2013/11/22
	 * This class will be the root of a set of recursive visitors.
	 */

public class PreVisitSetupVisitor<A> extends VoidVisitorAdapter<A>{

	private InnerClassMapper mapper;
	
	// For this compilation unit
	private String packageName;

	
	
	// Constructor
	public PreVisitSetupVisitor (InnerClassMapper icm) {
		
		mapper = icm;
		
	}
	
	
	@Override
	public void visit (ClassOrInterfaceDeclaration n, A arg) {
	    
		CompilationUnit containerCompliationUnit = (CompilationUnit) arg;
		
		// Get package
		PackageDeclaration pd = containerCompliationUnit.getPackage();
		packageName = pd.getName().toString();
		
        // Define the current type being looked at
        TypeDefinition currentVisitedType = new TypeDefinition(n.getName(), packageName);
        
        // Check for possible inner classes
        List<BodyDeclaration> members = n.getMembers();
        
        for (BodyDeclaration bd : members) {
            
            if (bd instanceof ClassOrInterfaceDeclaration) {
                
                ClassOrInterfaceDeclaration c = (ClassOrInterfaceDeclaration) bd;
                TypeDefinition innerClassFullName = 
                    TypeDefinition.createInnerClassDefinition(c.getName(), n.getName(), packageName);
                
                mapper.logInnerClass(innerClassFullName, currentVisitedType);
                
            }
            
        }
		
	}
	
	
}
